package com.lemner.motion.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemner.motion.R;
import com.lemner.motion.base.BaseActivity;
import com.lemner.motion.base.BaseListener;
import com.lemner.motion.base.BaseModel;
import com.lemner.motion.base.URL;
import com.lemner.motion.bean.LoginBean;
import com.lemner.motion.util.FullScreen;
import com.lemner.motion.util.SharedPreferencesUtils;

public class IndexActivity extends BaseActivity {
    private Button index_btn_login;
    private TextView index_tv_login,index_tv_reg;
    private long exitTime = 0;
    private String phone=null;
    private String pwd=null;
    private SharedPreferencesUtils util;

    @Override
    public int getLayoutFile()  {
        return R.layout.activity_index;
}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView() {
            FullScreen.fullScreen(this,0x00131313);
           index_btn_login=(Button)findViewById(R.id.index_btn_login);
           index_tv_login=(TextView)findViewById(R.id.index_tv_login1);
           index_tv_reg=(TextView)findViewById(R.id.index_tv_reg);
            util=new SharedPreferencesUtils(this);
    }

    @Override
    public void initEvent() {
        index_btn_login.setOnClickListener(this);
        index_tv_reg.setOnClickListener(this);
        index_tv_login.setOnClickListener(this);
    }

    @Override
    public void initData() {
        phone = util.readString("phone");
        pwd = util.readString("pwd");
        if (phone != null || pwd != null) {
            if (!phone.equals("") || !pwd.equals("")) {
                BaseModel<LoginBean> model = new BaseModel<>();
                retrofit2.Call<LoginBean> call = model.service.getLogin(phone, URL.md5(pwd));
                model.callEnqueue(call, listener);
            }
        }
    }



    BaseListener<LoginBean> listener=new BaseListener<LoginBean>() {
        @Override
        public void onResponse(LoginBean loginBean) {
            if(loginBean.getReturnCode().equals("0001")){
                LoginActivity.studentid=loginBean.getData().get(0).getStudentId();
                LoginActivity.session=loginBean.getData().get(0).getSessionId();
                showToast("登陆成功");
                startActivity(new Intent(IndexActivity.this, StartRunActivity.class));
                finish();
            }
            else
                showToast("您的密码有误，请检查后再试");
        }

        @Override
        public void onFail(String msg) {
            showToast(msg);
        }
    };


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.index_btn_login:
                startActivity(new Intent(IndexActivity.this,LoginActivity.class));
                break;
            case  R.id.index_tv_login1:
                break;
            case R.id.index_tv_reg:
                startActivity(new Intent(IndexActivity.this,Register1Activity.class));
                break;
            default:
                break;
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                showToast("再按一次退出主界面");
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
